import java.util.*;

import de.umass.lastfm.*;

public class LastfmAPI {
	
	static String apiKey = "49cf10db3cd5829fece99eaf791df694";
	static String artist_name;

	
		public Collection<Artist> showSimilarArtists(String artist_name){		
			Collection<Artist> similar = Artist.getSimilar(artist_name, 20, apiKey);
			for(Artist artist : similar){
				 artist.getName();
				 //System.out.println(artist.getName()); //comment out when calling the function
			}
			return similar;
		}


		public static String showArtistInfo(String artist_name){

			Artist artist_info = Artist.getInfo(artist_name, apiKey);
			
			
			if(artist_info == null)
				return "";
				
			String info = artist_info.getWikiSummary();
			//System.out.println(info);
			
			return info;
		}
		
		public Collection<Track> showTopTracks(String artist_name){
			if(artist_name.equals(""))
				artist_name = "[unknown]";
			
			Artist temp = Artist.getCorrection(artist_name, apiKey);
			
			Collection<Track> topTracks;
			try {
				topTracks = Artist.getTopTracks(temp.getName(), apiKey);
			} catch (Exception e) {
				return showTopTracks("");
			}
			
			int i=0;
            for (Track track : topTracks) {          
            	if(i<5){
            	 track.getName();	
                 //System.out.println(track.getName()); 
                 i++;
            }
          }         
            return topTracks;
    
	}
		
		public static Collection<Album> showTopAlbums(String artist_name){
			Collection<Album> topAlbums = Artist.getTopAlbums(artist_name, apiKey);
				int i=0;
				for(Album album : topAlbums){
					if(i<5){
					album.getName();
					//System.out.println(album.getName()); 
					i++;
				}
			}
				return topAlbums;
		}
		
		//TODO convert to PImage
		public static PaginatedResult<Image> displayImage(String artist_name){
			PaginatedResult<Image> artistImage = Artist.getImages(artist_name, 1, 1, apiKey);
			return artistImage;		
		}
		
		public static String[] weeklyTopArtists(){
			PaginatedResult<Artist> chart = Chart.getTopArtists(apiKey);
			Collection<Artist> display = chart.getPageResults();
			display.toArray();
			int i=0;
			String[] weeklyTop = new String[20];
			
			for(Artist artist : display){
				if(i<20){	
					//System.out.println(artist.getName()); 
					weeklyTop[i++] = artist.getName();;
			}
		}			
			return weeklyTop;		
		}
		
		public static String[] searchTrack(String testSong){
			Collection<Track> trackCollection = Track.search(testSong, apiKey);
			
			if(trackCollection.size() == 0)
				return null;
				
			String[] searchResult = new String[trackCollection.size()*2];
			int count = 0;
			
			for(Track track : trackCollection)
			{
				searchResult[count] = track.getName();
				searchResult[count+1] = track.getArtist();
				
				count+=2;
			}
			
			return searchResult;
		}
		
		public static Artist searchArtist(String testArtist){
			Artist temp = Artist.getCorrection(testArtist, apiKey);
			//System.out.println(temp.getName());
			return temp;
		}
		
		public static String[] searchArtistAndTrack(String testArtist, String testSong){
			
			Artist tempArtist = Artist.getCorrection(testArtist, apiKey);
			tempArtist = Artist.getInfo(testArtist, apiKey);
			testArtist = tempArtist.getName();
			Track testTrack = Track.getInfo(testArtist, testSong, apiKey);
			
			if(testTrack == null) 
				return new String[8];
			
			String[] track_info = new String[8];
			track_info[0] = testTrack.getName();
			track_info[1] = testTrack.getArtist();
			track_info[2] = testTrack.getAlbum();
			track_info[3] = "" + testTrack.getDuration();
			track_info[4] = testTrack.getUrl();
			track_info[5] = tempArtist.getUrl();
			track_info[6] = "" + testTrack.getPlaycount();
			track_info[7] = testTrack.getWikiSummary();
			
			
			/*System.out.println("Track: " + track_info[0]);
			System.out.println("Artist: " + track_info[1]);
			System.out.println("Album: " + track_info[2]);
			System.out.println("Duration: " + track_info[3] + " seconds");
			System.out.println("Play Count: " + track_info[6]);
			System.out.println("Song Url: " + track_info[4]);
			System.out.println("Artist Url: " + track_info[5]);
			System.out.println("Info: " + track_info[7]);*/
			
			return track_info;
		}

		
		public String[] getTopTrackNames(String artist_name) {
	           Track[] trackArray = showTopTracks(artist_name).toArray(new Track[6]);
	           String[] trackNames = new String[5];
	           
	           if(trackArray[0] != null && trackArray[0].getArtist().equals("[unknown]")) {
	        	   for(int c=0; c<5; c++) {
	        		   trackNames[c] = "";
	        	   }
	        	   return trackNames;
	           }
	           
	           for(int x=0; x<trackNames.length; ++x) {
	        	   if(trackArray[x] != null) {
	        		   trackNames[x] = trackArray[x].getName();
	        	   }
	           }	           
	           return trackNames;
		}
		
		public int[] getSimilarArtists(String artist_name) {
			System.out.println(artist_name);
			Artist[] artistArray = (Artist[]) showSimilarArtists(artist_name).toArray(new Artist[50]);
			int[] artists = new int[5];
			
			if(artistArray == null){
				return artists;
			}
			
			int index = 0;
			for(int x=0; x<artistArray.length; ++x) {
				if(artistArray[x] == null || index > 4)
					break;
					
					
				int globalIndex = GLOBAL.findArtist(artistArray[x].getName().toLowerCase());
				if(!GLOBAL.IGNORE.contains(artistArray[x].getName()) && globalIndex > -1 && !artistArray[x].getName().equals(artist_name)) {
					artists[index] = globalIndex;
					index++;
				}
			}

			if(index < 4) {
				for(int y=index; y<4; ++y) {
					artists[y] = -1;
				}
			}
			
			return artists;
		}
	
}
